package com.kalaqia.wechat.auth.di;

import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.MainThreadDisposable;
import io.reactivex.internal.schedulers.IoScheduler;
import io.reactivex.schedulers.Schedulers;

/**
 * function:
 * project:WeChatModule
 * describe:
 * created owner:kalaqia 421407393@qq.com
 * created date:2017/9/1 15:26
 */
//<editor-fold desc="version update log">
// updare owner:kalaqia  421407393@qq.com
// update datetime:2017/9/1 15:26 
// update remark:
//</editor-fold>
public class WeChatSendAuthRespObservable extends Observable<BaseResp> {

    @Override
    protected void subscribeActual(Observer<? super BaseResp> observer) {
        AuthDisposable disposable = new AuthDisposable(observer);
        observer.onSubscribe(disposable);
    }

    private static class AuthDisposable extends MainThreadDisposable implements IWXAPIEventHandler {

        private final Observer<? super BaseResp> observer;

        public AuthDisposable(Observer<? super BaseResp> observer) {
            this.observer = observer;
        }

        @Override
        public void onReq(BaseReq baseReq) {

        }

        @Override
        public void onResp(BaseResp baseResp) {
            if (baseResp.errCode == BaseResp.ErrCode.ERR_OK) {
                observer.onNext(baseResp);
            } else {
                observer.onError(new Throwable(baseResp.errStr));
            }
        }

        @Override
        protected void onDispose() {

        }

    }

}
